package com.yunji.hushuang;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author hushuang
 * @since 2019/9/29
 */
public class ThreadLocalSingleton {

    private static ThreadLocal<ThreadLocalSingleton> threadLocal = ThreadLocal.withInitial(ThreadLocalSingleton::new);

    public static void main(String[] args) {
        System.out.println(threadLocal.get());
        System.out.println(threadLocal.get());
        System.out.println(threadLocal.get());

        final int threadCount = 3;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
        for (int i = 0; i < threadCount; i++) {
            executorService.submit(() -> {
                System.out.println(Thread.currentThread().getId() + " " + threadLocal.get());
                System.out.println(Thread.currentThread().getId() + " " + threadLocal.get());
                System.out.println(Thread.currentThread().getId() + " " + threadLocal.get());
                threadLocal.remove();
            });
        }
        threadLocal.remove();
        executorService.shutdown();
    }


}
